package com.essc.serviceImpl;

import com.essc.mapper.BookmarkMapper;
import com.essc.mapper.CatalogMapper;
import com.essc.mapper.GoodsMapper;
import com.essc.mapper.UserMapper;
import com.essc.pojo.Bookmark;
import com.essc.pojo.Catalog;
import com.essc.pojo.Goods;
import com.essc.service.BookmarkService;
import com.essc.utils.JsonResult;
import com.essc.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookmarkServiceImpl implements BookmarkService {

    @Autowired
    private BookmarkMapper bookmarkMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodMapper;
    @Autowired
    private CatalogMapper catalogMapper;

    /*用户查看个人收藏商品*/
    @Override
    public JsonResult findBookmarkByUid(Integer uid, Integer pageNum, Integer pageSize) {
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum, pageSize);
        List<Bookmark> bookmarkList = bookmarkMapper.findAllBookmarkByUid(uid);
        for (int i = 0; i < bookmarkList.size(); i++) {
            Bookmark bookmark2 = new Bookmark();
            Bookmark bookmarkli = bookmarkList.get(i);
            //获取商品id
            Integer gid = bookmarkli.getGoodId();
            //获取商品信息
            Goods goods = goodMapper.findGoodById(gid);
            //获取商品名
            String gname = goods.getGname();
            //获取商品价格
            Double price = goods.getPrice();
            //获取商品图片
            String pic = goods.getPic();
            //获取商品状态
            Integer status = goods.getStatus();
            //获取商品描述
            String remark = goods.getRemark();
            //获取商品类别
            Integer cid = goods.getCid();
            Catalog catalog = catalogMapper.findCatalogByCid(cid);
            String cname = catalog.getCname();
            //将收藏商品信息注入
            bookmark2.setBid(bookmarkli.getBid());
            bookmark2.setUserId(bookmarkli.getUserId());
            bookmark2.setCid(cid);
            bookmark2.setCname(cname);
            bookmark2.setGoodId(gid);
            bookmark2.setGname(gname);
            bookmark2.setPrice(price);
            bookmark2.setPic(pic);
            bookmark2.setStatus(status);
            bookmark2.setRemark(remark);
            bookmarkList.set(i, bookmark2);
        }
        int totalSize = (int) ((Page) bookmarkList).getTotal();
        PageResult pageResult = new PageResult(pageNum, pageSize, totalSize, bookmarkList);
        if (bookmarkList.size() == 0) {
            return new JsonResult(202, "快去收藏您喜欢的商品吧~");
        }
        return new JsonResult(200, pageResult);
    }

    /*用户新增收藏商品 在商品详情页面实现*/
    @Override
    public JsonResult addBookmark(Bookmark bookmark) {
        List<Bookmark> bookmarkList = bookmarkMapper.findAllBookmarkByUid(bookmark.getUserId());
        for (int i = 0; i < bookmarkList.size(); i++) {
            Bookmark bookmarkli = bookmarkList.get(i);
            Integer goodId = bookmarkli.getGoodId();
            if (bookmark.getGoodId().equals(goodId)) {
                return new JsonResult(201, "不可重复收藏该商品");
            }
        }
        int addrow = bookmarkMapper.addBookmark(bookmark);
        int gid = bookmark.getGoodId();
        goodMapper.updateGoodsWeight(gid);

        if (addrow != 1) {
            return new JsonResult(202, "商品不存在");
        }
        return new JsonResult(200, "商品已添加至收藏", addrow);
    }

    /*用户将收藏商品移除收藏 在收藏夹实现*/
    @Override
    public JsonResult delBookmarkByBid(Bookmark bookmark) {
        int delrow = bookmarkMapper.delBookmarkByBid(bookmark);
        int gid = bookmark.getGoodId();
        goodMapper.reduceGoodsWeight(gid);
        if (delrow != 1) {
            return new JsonResult(201, "商品不存在");
        }
        return new JsonResult(200, "商品已被移除收藏", delrow);
    }

    /*用户将收藏商品批量移除收藏 在收藏夹实现*/
    @Override
    public JsonResult delBookmarks(String[] bids) {
        int delrows = bookmarkMapper.delBookmarks(bids);
        if (delrows < 1) {
            return new JsonResult(201, "商品不存在");
        }
        return new JsonResult(200, "所选商品已被移除收藏", delrows);
    }

    @Override
    public Boolean findBookmark(Integer uid, Integer gid) {
        Bookmark bookmark = bookmarkMapper.findBookmark(uid, gid);
        if (bookmark == null) {
            return false;
        }
        return true;
    }
}
